package com.datam.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import ch.qos.logback.classic.Logger;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.datam.dao.CompanyInfoDAO;
import com.datam.dao.CompanyPkInfoDAO;
import com.datam.dao.SysUserDAO;
import com.datam.dao.YJDAO;
import com.datam.domain.CompanyInfoDO;
import com.datam.domain.CompanyPkInfoDO;
import com.datam.domain.CompanyPkYjDO;
import com.datam.domain.ConpanyTxInfoDO;
import com.datam.domain.SysUserDO;
import com.datam.query.QueryCompanyInfo;
import com.datam.query.QueryCompanyPkInfo;
import com.datam.query.QueryConpanyTxInfo;
import com.datam.query.QuerySysUser;
import com.datam.utils.DMResult;


@Controller
public class YJController {
	
	@Autowired
	YJDAO yJDAO ; 
	
	@Autowired
	CompanyPkInfoDAO companyPkInfoDAO ; 
	
	@Autowired
	CompanyInfoDAO companyInfoDAO;
	
	@RequestMapping("AlertSetting/GetAlertSetting")
	public @ResponseBody Object GetAlertSetting(HttpServletRequest request, HttpServletResponse response,
			Long CompanyID){
		try {
			Map data  = new HashMap();
			List<Map> zyj = yJDAO.selectZyjByCid(CompanyID);
			if(zyj!=null&&zyj.size()>0)
				data.put("cas", zyj.get(0));
			QueryCompanyPkInfo QueryCompanyPkInfo = new QueryCompanyPkInfo();
			QueryCompanyPkInfo.setCOMPANYID(CompanyID);
			List< CompanyPkInfoDO >  padata = companyPkInfoDAO.selectCompanyPkInfoList(QueryCompanyPkInfo);
			data.put("casi", padata);
			return new DMResult(data,"操作成功",true);
		}catch (Exception e) {
			e.printStackTrace();
			return new DMResult("操作失败",false);
		}
	}
	
	@RequestMapping("AlertSetting/SaveAlertSetting")
	public @ResponseBody Object SaveAlertSetting(HttpServletRequest request, HttpServletResponse response,
			Long ID,Long CompanyID,String STATE,String OFFLINETIME,String YEARPERCENT,String NOCHANGEHOUR,	String NOCHANGEPERCENT){
		try {
			if(ID==null){
				yJDAO.AddZyj(CompanyID,STATE,OFFLINETIME,YEARPERCENT,NOCHANGEHOUR,NOCHANGEPERCENT);
			}else{
				yJDAO.upZyj(ID,CompanyID,STATE,OFFLINETIME,YEARPERCENT,NOCHANGEHOUR,NOCHANGEPERCENT);

			}
			return new DMResult("操作成功",true);
		}catch (Exception e) {
			e.printStackTrace();
			return new DMResult("操作失败",false);
		}
	}
	
	
	@RequestMapping("AlertSetting/GetPKAlertSetting")
	public @ResponseBody Object GetPKAlertSetting(HttpServletRequest request, HttpServletResponse response,
			long PKID){
		try {
			List<Map> data = yJDAO.selectpkyjBypkid(PKID);
			Map r = new HashMap();
			r.put("total", data.size());
			r.put("rows", data);
			return r ;
		}catch (Exception e) {
			e.printStackTrace();
			return new DMResult("操作失败",false);
		}
	}
	
	@RequestMapping("AlertSetting/AddPKAlertSetting")
	public @ResponseBody Object AddPKAlertSetting(HttpServletRequest request, HttpServletResponse response,
			String data){
		try {
			List<CompanyPkYjDO> t = JSON.parseArray(data, CompanyPkYjDO.class);
			System.out.println("data.size="+t.size());
			for(CompanyPkYjDO companyPkYjDO : t ){
				if(companyPkYjDO.getID() == null){
					yJDAO.insertpkyj(companyPkYjDO);
				}else{
					yJDAO.updatepkyjByDo(companyPkYjDO);
				}
			}
			return new DMResult("操作成功",true);
		}catch (Exception e) {
			e.printStackTrace();
			return new DMResult("操作失败",false);
		}
	}
	
	@RequestMapping("AlertSetting/UpdatePKAlertSetting")
	public @ResponseBody Object UpdatePKAlertSetting(HttpServletRequest request, HttpServletResponse response,
			Long ID, String STATE){
		try {
			yJDAO.updatepkyj(ID,STATE);
			return new DMResult("操作成功",true);
		}catch (Exception e) {
			e.printStackTrace();
			return new DMResult("操作失败",false);
		}
	}
	
	@RequestMapping("/VOCsCompany/SaveYearPermitList")
	public @ResponseBody Object SaveYearPermitList(HttpServletRequest request, HttpServletResponse response,
			Long ID, long COMPANYID,String ITEMCODE,String STATE,String VALUE){
		try {
			if(ID==null){
				yJDAO.insertyj(ID,COMPANYID,ITEMCODE,STATE,VALUE);
			}else{
				yJDAO.updateyj(ID,COMPANYID,ITEMCODE,STATE,VALUE);
			}
			
			return new DMResult("操作成功",true);
		}catch (Exception e) {
			e.printStackTrace();
			return new DMResult("操作失败",false);
		}
	}
	
	@RequestMapping("UserControl/GetPKTree")
	public @ResponseBody Object GetPKTree(HttpServletRequest request, HttpServletResponse response,
			String cType ,String pType,String keyWord	){
		try {
			QueryCompanyPkInfo QueryCompanyPkInfo = new QueryCompanyPkInfo();
			SysUserDO user = (SysUserDO) request.getSession().getAttribute("user");
			List< CompanyPkInfoDO >  padata = new ArrayList<>();
			QueryCompanyPkInfo.setTYPE(pType);
			if(user.getType()==1){
				QueryCompanyInfo queryCompanyInfo = new QueryCompanyInfo();
				queryCompanyInfo.setUSERID(user.getUserId());
				List< CompanyInfoDO > cdata = companyInfoDAO.selectCompanyInfoList(queryCompanyInfo);
				for(CompanyInfoDO c : cdata ){
					QueryCompanyPkInfo.setCOMPANYID(c.getID());
					padata.addAll(companyPkInfoDAO.selectCompanyPkInfoList(QueryCompanyPkInfo));
				}
			}
			String cid = (String) request.getSession().getAttribute("userCid");

			if(user.getType()==2){
				QueryCompanyPkInfo.setCOMPANYID(Long.valueOf(cid));
				padata.addAll(companyPkInfoDAO.selectCompanyPkInfoList(QueryCompanyPkInfo));
			}
			if(user.getType()==0){
				padata.addAll(companyPkInfoDAO.selectCompanyPkInfoList(QueryCompanyPkInfo));
			}
			Map data = new HashMap();
			List one = new ArrayList();
			data.put("text", "请选择("+padata.size()+")");
			data.put("state", "open");
			data.put("id", "-1");
			data.put("attributes", "{\"Type\":\"\"}");
			Map pkMap = new HashMap();
			Map<String,String> gszt = new HashMap<String,String>();
			
			Map<String,String> gslzt = new HashMap<String,String>();
			if(padata!=null){
				for(CompanyPkInfoDO pk : padata){
					List tmp = (List) pkMap.get(pk.getCOMPANYID());
					if(tmp ==null){
						tmp = new ArrayList();
						pkMap.put(pk.getCOMPANYID(), tmp);
					}
					Map temMap = new HashMap();
					temMap.put("text", pk.getNAME());
					temMap.put("state", "open");
					temMap.put("id", "P"+pk.getID());
					temMap.put("iconCls", pk.getIconCls());
					temMap.put("attributes", "{\"Type\":\""+pk.getTYPE()+"\"}");
					temMap.put("children",new ArrayList<>());
					tmp.add(temMap);
					if("vocs_red".equals(pk.getIconCls())){
						gszt.put(pk.getCOMPANYID()+"", "vocs_red");
					}
					if("vocs_green".equals(pk.getIconCls())){
						gslzt.put(pk.getCOMPANYID()+"", "vocs_green");
					}
					
				}
			}
			
			List<String> CompanyIds = new ArrayList<String>();
			QueryCompanyInfo queryCompanyInfo = new QueryCompanyInfo();
			queryCompanyInfo.setSHOWTYPE("2");
			if(keyWord!=null)
				queryCompanyInfo.setNAME(keyWord);
			if(user.getType()==1){
				queryCompanyInfo.setUSERID(user.getUserId());
			}
			if(user.getType()==2){
				queryCompanyInfo.setID(Long.valueOf(cid));
			}
			List< CompanyInfoDO > cdata = companyInfoDAO.selectCompanyInfoList(queryCompanyInfo);
			if(cdata!=null){
				for(CompanyInfoDO ci : cdata){
					Map temMap = new HashMap();
					temMap.put("text", ci.getNAME());
					temMap.put("state", "open");
					temMap.put("id", "C"+ci.getID());
					if(gszt.get(ci.getID()+"")!=null){
						temMap.put("iconCls", "company_red");
					}else if(gslzt.get(ci.getID()+"")!=null){
						temMap.put("iconCls", "company_green");
					}else{
						temMap.put("iconCls", "company_gray");
					}
					data.put("attributes", "{\"Type\":\"\"}");
					List pklist = (List) pkMap.get(ci.getID());
					if(pklist ==null)pklist = new ArrayList<>();
					temMap.put("children",pklist);
					one.add(temMap);
					CompanyIds.add(ci.getID()+"");
				}
			}
			data.put("children", one);
			Map r = new HashMap();
			r.put("CompanyIds", CompanyIds.toArray());
			List t = new ArrayList();
			t.add(data);
			r.put("TreeData", t);
			return r ;
		}catch (Exception e) {
			e.printStackTrace();
			return new DMResult("操作失败",false);
		}
	}
	
	
	
	@RequestMapping("/StainMonitor/GetDataList")
	public @ResponseBody Object GetDataList(HttpServletRequest request, HttpServletResponse response,
			Long ID, Long TYPE,String BeginTime,String EndTime	,String DataType){
		try {
			if(DataType =="M")DataType="2051";
			if(DataType =="H")DataType="2061";
			if(DataType =="D")DataType="2041";
			if(TYPE == 1){
				
			}
			return new DMResult("操作成功",true);
		}catch (Exception e) {
			e.printStackTrace();
			return new DMResult("操作失败",false);
		}
	}
	
	@RequestMapping("/VOCsCompany/GetWXInfoList")
	public @ResponseBody Object GetWXInfoList(HttpServletRequest request, HttpServletResponse response,
			String COMPANYID){
		try {
			List<Map> data = yJDAO.GetWXInfoList(COMPANYID);
			Map r = new HashMap();
			r.put("total", data.size());
			r.put("rows", data);
			return r;
		}catch (Exception e) {
			e.printStackTrace();
			return  new HashMap();
		}
	}
	
	@Autowired
	SysUserDAO sysUserDAO ; 
	
	@RequestMapping("/VOCsCompany/delWXInfoByID")
	public @ResponseBody DMResult delWXInfoByID(HttpServletRequest request, HttpServletResponse response,
			String COMPANYID,String ID){
		try {
			yJDAO.deleteWXInfo(COMPANYID,ID);
			QuerySysUser querySysUser = new QuerySysUser();
			querySysUser.setCompanyInfo(COMPANYID);
			List< SysUserDO > d = sysUserDAO.selectSysUserList(querySysUser);
			if(d ==null || d.size()==0)  return new DMResult("用户不存在");
			SysUserDO user = d.get(0);
			List<String> wx = new ArrayList<String>();
			String wxids = user.getWxid();
			if(StringUtils.isNotBlank(wxids)){
				List<String>  tmp = JSON.parseArray(wxids, String.class);
				for(String s : tmp){
					if(!ID.equals(s)){
						wx.add(s);
					}
				}
				user.setWxid(JSONArray.toJSONString(wx));
				sysUserDAO.updateSysUserByUserId(user);
			}
			return new DMResult("操作成功",true);
		}catch (Exception e) {
			e.printStackTrace();
			return new DMResult("操作失败",false);
		}
	}
	
}
